package com.colin.app.pay.weixin;

import android.app.Activity;
import android.widget.Toast;

import com.colin.app.pay.callback.IPayListener;
import com.colin.library.help.LogHelp;
import com.tencent.mm.opensdk.modelpay.PayReq;
import com.tencent.mm.opensdk.openapi.IWXAPI;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by ColinLu on 2016/9/8.
 */
public class WXPayUtil {

    // APP_ID 替换为你的应用从官方网站申请到的合法appId
    public static final String APP_ID = "wxd930ea5d5a258f4f";
    public static final String WX_APP_ID = "wxb4ba3c02aa476ea1";
    //    public static final String WX_APP_ID = "a878b8013c47b087ab66bef1851b1790";
    private static final String WX_SECRET = "f66745d18ba491878b1477194ce47262";
    private static final String WX_MCH_ID = "1238457102";
    private static final String WX_API_KEY = "d01c5577c5748e79ee40bfc6f67d124d";
    private static final String PAY_URL = "http://wxpay.weixin.qq.com/pub_v2/app/app_pay.php?plat=android";

    private static WXPayUtil instance = null;
    private Activity mActivity;
    private IPayListener mListener = null;

    private WXPayUtil() {
    }

    public static class Holder {
        static WXPayUtil instance = new WXPayUtil();
    }

    public static WXPayUtil getInstance() {
        return Holder.instance;
    }

    public void pay(final Activity mActivity, Object o, final IPayListener mListener) {

        String appid = "wxfac2b15cf7763717";
        String partnerid = "1234703702";
        String prepayid = "wx2017061510143243739ca86d0077962349";
        String noncestr = "c6bff625bdb0393992c9d4db0c6bbe45";
        String timestamp = "1497492872";
        String sign = "A51E1A1F2D7CA62619E4E1A9A2C51D75";
        WechatPayReq wechatPayReq = new WechatPayReq.Builder()
                .with(mActivity) //activity实例
                .setAppId(appid) //微信支付AppID
                .setPartnerId(partnerid)//微信支付商户号
                .setPrepayId(prepayid)//预支付码
                .setNonceStr(noncestr)
                .setTimeStamp(timestamp)//时间戳
                .setSign(sign)//签名
                .setOnPayListener(mListener)
                .create();
        wechatPayReq.send();


//        this.mListener = mListener;
//        final IWXAPI api = WXAPIFactory.createWXAPI(mActivity, WX_APP_ID, false);
//        api.registerApp(WX_APP_ID);
//        //先判断是否支持微信支付
//        OkHttpUtils.get().url(PAY_URL)
//                .build().execute(new StringCallback() {
//            @Override
//            public void onError(Call call, Exception e, int id) {
//                if (mListener != null) {
//                    mListener.payCallBack(IPayListener.FAIL, "网络请求失败");
//                }
//            }
//
//            @Override
//            public void onResponse(String response, int id) {
//                parseJson(mActivity, api, response, mListener);
//
//            }
//        });
    }

    /**
     * 支付 调用接口返回数据
     *
     * @param mActivity
     * @param response
     * @param mListener
     */
    private void parseJson(Activity mActivity, IWXAPI api, String response, IPayListener mListener) {
        LogHelp.e("get server pay params:", response);
        JSONObject json = null;
        try {
            json = new JSONObject(response);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        if (null == json) {
            if (mListener != null) {
                mListener.payCallBack(IPayListener.FAIL, "网络请求失败");
            }
            return;
        }
        if (!json.has("retcode")) {
            PayReq req = new PayReq();
            //req.appId = "wxf8b4f85f3a794e77";  // 测试用appId
            req.appId = json.optString("appid", "");
            req.partnerId = json.optString("partnerid", "");
            req.prepayId = json.optString("prepayid", "");
            req.nonceStr = json.optString("noncestr", "");
            req.timeStamp = json.optString("timestamp", "");
            req.packageValue = json.optString("package", "");
            req.sign = json.optString("sign", "");
            req.extData = "app data"; // optional
            Toast.makeText(mActivity, "正常调起支付", Toast.LENGTH_SHORT).show();
            // 在支付之前，如果应用没有注册到微信，应该先调用IWXMsg.registerApp将应用注册到微信
            api.sendReq(req);
        } else {
            LogHelp.d("PAY_GET", "返回错误" + json.optString("retmsg"));
            if (mListener != null) {
                mListener.payCallBack(IPayListener.FAIL, json.optString("retmsg"));
            }
        }
    }
}
